package com.ddm.controller.wxadmin;


import com.ddm.constant.MessageConstant;
import com.ddm.dto.page.PageQueryDTO;
import com.ddm.dto.vxDTO.UserDTO;
import com.ddm.entity.vxEntity.User;
import com.ddm.exception.DataErrorException;
import com.ddm.properties.JwtProperties;
import com.ddm.properties.OpenProperties;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.vxService.UserService;
import com.ddm.utils.BeanCopyUtil;
import com.ddm.vo.vxVo.UserVo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author horizon
 * @since 2025-01-17
 */
@RestController
@RequestMapping("/admin/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private OpenProperties openProperties;


    /**
     * 分页获取用户列表
     */
    @GetMapping("/userList")
    @ApiOperation("分页获取用户列表")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getUserList(PageQueryDTO pageQueryDTO){
        log.info("分页获取用户列表");
        return Result.success(userService.getUserList(pageQueryDTO));
    }


    /**
     * 根据id查询用户
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public Result<UserVo> getById(@PathVariable Long id){
        log.info("根据id查询用户：{}",id);
        return Result.success(userService.getByid(id));

    }

    @DeleteMapping
    @ApiOperation("批量删除用户")
    public Result deleteUserList(@RequestBody List<UserDTO> userDTOS ){
        log.info("批量删除用户");
        if (userDTOS == null){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        userService.deleteUserList(userDTOS);

        return Result.success("删除成功");
    }

    @PutMapping("/{id}")
    @ApiOperation("根据id修改用户类型")
    public Result<UserVo> updateUserType(@PathVariable Long id, Integer userType){
        log.info("修改用户类型");
        User user = userService.getById(id);
        if (userType == null ||user == null||user.getUserType() == userType){
            throw  new DataErrorException(MessageConstant.ERROR_DATA);
        }
        user.setUserType(userType);
        userService.updateById(user);
        UserVo userVo = BeanCopyUtil.BeanCopy(user, UserVo.class);
        return Result.success(userVo);
    }


}

